package com.ruoyi.web.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api("用户管理")
@Controller
@RequestMapping("/api/user")
public class UserController{

	@Autowired
	private ISysUserService sysUserService;

	@ApiOperation("项目经理-可派单-派单-获取订单接收人列表")
	@ApiImplicitParam(name = "deptId", value = "部门id", required = false, dataType = "Long")
	@GetMapping("/getUserList")
	@ResponseBody
	public AjaxResult getUserList(Long deptId) throws Exception{
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("deptId", deptId);
		List<SysUser> userList = sysUserService.selectUserByParams(paramsMap);

		// 遍历比较
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(userList != null && userList.size() > 0){
			for(SysUser user : userList){
				String deptName = user.getDept().getDeptName();
				if(resultMap.containsKey(deptName)){
					Map<String, Object> tempMap = new HashMap<String, Object>();
					tempMap.put("userId", user.getUserId());
					tempMap.put("label", user.getUserName());
					tempMap.put("phoneNumber", user.getPhonenumber());
					((List<Map<String, Object>>)resultMap.get(deptName)).add(tempMap);
				}else{
					Map<String, Object> tempMap = new HashMap<String, Object>();
					tempMap.put("userId", user.getUserId());
					tempMap.put("label", user.getUserName());
					tempMap.put("phoneNumber", user.getPhonenumber());
					List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
					tempList.add(tempMap);
					resultMap.put(deptName, tempList);
				}
			}
		}

		// 格式化输出
		List<Map> resultList = new ArrayList<Map>();
		if(resultMap != null && !resultMap.isEmpty()){
			for(String key : resultMap.keySet()){
				Map<String, Object> tempMap = new HashMap<String, Object>();
				tempMap.put("label", key);
				tempMap.put("list", (List)resultMap.get(key));
				resultList.add(tempMap);
			}
		}
		return AjaxResult.success(resultList);
	}

}
